﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;

#endregion

namespace NsbMonitorClientService.QueueManager
{
    public class BasicQueueReader : IQueueReader
    {
        public IEnumerable<Message> ReadMessagesFromQueue(String queueFullPath)
        {
            if (!MessageQueue.Exists(queueFullPath))
            {
                return Enumerable.Empty<Message>();
            }

            using (MessageQueue q = new MessageQueue(queueFullPath))
            {
                q.MessageReadPropertyFilter.SetAll();
                return q.GetAllMessages();
            }
        }

        public Message ReadMessageById(string queueFullPath, string messageId)
        {
            if (!MessageQueue.Exists(queueFullPath))
            {
                return null;
            }

            try
            {
                using (MessageQueue q = new MessageQueue(queueFullPath))
                {
                    Message message = q.ReceiveById(messageId);

                    return message;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
}